/** 公共组件 */
const ALERT_COMMON =Swal.mixin({
                        // 确认按钮文本
                        confirmButtonText: "确认",
                        // 取消按钮文本
                        cancelButtonText: "关闭",
                        // 拒绝按钮文本
                        denyButtonText: "拒绝",
                        // 确认按钮颜色
                        confirmButtonColor: "#e5bf08",
                    });

/** 提示类消息公共组件 */
const AlertMsg =    ALERT_COMMON.mixin({
                        // 延时2s自动消失
                        timer: 1500,
                        // 小通知类组件？
                        toast: true,
                        // 是否展示确认按钮
                        showConfirmButton: false,
                        // 是否展示取消按钮
                        showCancelButton: false,
                        // 时间条
                        timerProgressBar: true,
                    });

/** 操作类提示公共组件 */
const AlertOperation =  ALERT_COMMON.mixin({
                            // 是否展示确认按钮
                            showConfirmButton: true,
                            // 是否展示取消按钮
                            showCancelButton: true
                        });

const AlertLoading =    ALERT_COMMON.mixin({
                            title: "努力加载中",
                            // 小通知类组件？
                            toast: true,
                            // 是否展示确认按钮
                            showConfirmButton: false,
                            // 是否展示取消按钮
                            showCancelButton: false,
                            // 时间条
                            timerProgressBar: true,
                            loaderHtml: 'G',
                            // 自定义样式
                            customClass:{
                            },
                            timer: 15000,
                        });

const __alert = {
    "success": function(title, text, timer) { 
        return AlertMsg.fire({
            title: title,
            text: text,
            icon: "success",
            timer: timer || 1500,
        }); },
    "info":    function(title, text, timer) { 
        return AlertMsg.fire({
            title: title,
            text: text,
            icon: "info",
            timer: timer || 3000,
        }); 
    },
    "error":   function(title, text, timer) { 
        return AlertMsg.fire({
            title: title,
            text: text,
            icon: "error",
            timer: timer || null,
            showCloseButton: true,
        }); 
    },

    "warning": function(option) { 
        option.icon = "warning";
        return AlertOperation.fire(option); 
    },
    "question": function(option) { 
        option.icon = "question";
        option.showCancelButton = false;
        option.showDenyButton = true;
        return AlertOperation.fire(option); 
    },
    "form": function(form, option) {
        option.icon = null;
        option.html = form;
        return AlertOperation.fire(option); 
    },


    "showLoading": function(arg1, arg2, arg3) {
        var _ajax, _text, _timer, _option;
        var size = arguments.length;
        

        if (size == 1) {
            if ($.type(arg1) == "object") {
                _text = arg1.text;
                delete arg1.text;
                _timer = arg1.timer;
                delete arg1.timer;
                _option = arg1;
            }
        } else if (size == 2) {
            if ($.type(arg1) == "string") {
                _text = arg1;
            }
            _option = arg2;
        } else if (size == 3) {
            if ($.type(arg1) == "string" && $.type(arg2) == "number") {
                _text = arg1;
                _timer = arg2;
            }
            _option = arg3;
        }
        return  AlertLoading.fire({
            text: _text || '等待服务器数据加载...',
            timer: _timer || 15000,
            // 弹层打开
            didOpen: () => {
                Swal.showLoading();
                _ajax = ajax4Loading(_option);
            },
        }).then(function(result) {
            if (result.dismiss === Swal.DismissReason.timer) {
                _ajax.abort();
                __alert.error("加载异常", "等待服务器数据超时！");
            }  
        }); 
    } 
}

function ajax4Loading(option) {
    let type = option.type || "GET";
    let sucFuc = option.success;
    delete option.success;

    if (type === "GET") {

    } else {
        return __jqPostJson(option.url, option.data).done(function (res) {
            Swal.stopTimer();
            Swal.close();
            if (sucFuc)
                sucFuc(res);
        }).fail(function(err) {
            __alert.error("加载异常", "服务器处理异常！");
        });
    }
}
